import java.io.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class MtBullerResort {
    private FileInputStream fis;
    private ObjectInputStream ois;
    private FileOutputStream fos;
    private ObjectOutputStream oos;
    private ArrayList<Accommodation> accommodations;
    private ArrayList<Customer> customers;
    private ArrayList<TravelPackage> packages;

    public MtBullerResort() {
        accommodations = new ArrayList<>();
        customers = new ArrayList<>();
        packages = new ArrayList<>();
        // 加默认用户
        customers.add(new Customer(1,"aa","intermediate"));
        customers.add(new Customer(2,"bb","beginner"));
        customers.add(new Customer(3,"cc","expert"));
        // 加默认房间>10
        accommodations.add(new Accommodation(1, "Hotel", 100.0));
        accommodations.add(new Accommodation(2, "Lodge", 80.0));
        accommodations.add(new Accommodation(3, "Apartment", 120.0));
        accommodations.add(new Accommodation(4, "Hotel", 100.0));
        accommodations.add(new Accommodation(5, "Hotel", 100.0));
        accommodations.add(new Accommodation(6, "Hotel", 100.0));
        accommodations.add(new Accommodation(7, "Apartment", 120.0));
        accommodations.add(new Accommodation(8, "Hotel", 120.0));
        accommodations.add(new Accommodation(9, "Apartment", 120.0));
        accommodations.add(new Accommodation(10, "Lodge", 80.0));
        accommodations.add(new Accommodation(11, "Lodge", 100.0));
    }
    // 1、显示所有住宿
    public void displayAllAccommodations() {
        for (Accommodation accommodation : accommodations) {
            System.out.println(accommodation.getId() + " - " + accommodation.getType() + " - $" + accommodation.getPrice());
        }
    }

    // 2、显示可用住宿
    public void displayAvailableAccommodations() {
        for (Accommodation accommodation : accommodations) {
            if (accommodation.isAvailable()) {
                System.out.println(accommodation.getId() + " - " + accommodation.getType() + " - $" + accommodation.getPrice());
            }
        }
    }
    // 3、加个用户
    public void addCustomer() {
        try{
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter customer ID: ");
            int id = scanner.nextInt();
            scanner.nextLine(); // Consume newline
            System.out.print("Enter customer name: ");
            String name = scanner.nextLine();
            String level ;
            boolean validInput = false;
            do {
                System.out.print("Enter customer level (beginner/intermediate/expert): ");
                level = scanner.nextLine().toLowerCase();
                if (level.equals("beginner") || level.equals("intermediate") || level.equals("expert")) {
                    validInput = true;
                } else {
                    System.out.println("Invalid input. Please enter 'beginner', 'intermediate', or 'expert'.");
                }
            } while (!validInput);
            Customer customer = new Customer(id, name, level);
            customers.add(customer);
            System.out.println("Customer added successfully.");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    // 4、列出所有用户
    public void listCustomers() {
        System.out.println("List of Customers:");
        for (Customer customer : customers) {
            System.out.println(customer.getId() + " - " + customer.getName() + " - " + customer.getLevel());
        }
    }
    // 5、加个订单，给客户定住宿
    public void addPackage() {
        try {
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter customer ID: ");
            int customerId = scanner.nextInt();

            System.out.print("Enter start date (YYYY-MM-DD): ");
            String dateStr = scanner.next();
            LocalDate startDate = null;
            try{
                startDate = LocalDate.parse(dateStr);
            }
            catch(Exception e){
                e.printStackTrace();
            }

            System.out.print("Enter duration (in days): ");
            int duration = scanner.nextInt();

            System.out.println("Select accommodation:");
            displayAvailableAccommodations();

            System.out.print("Enter accommodation ID: ");
            int accommodationId = scanner.nextInt();
            Accommodation selectedAccommodation = findAccommodationById(accommodationId);
            Customer selectedcus = findCustom(customerId);

            if (selectedAccommodation != null && selectedAccommodation.isAvailable()) {
                TravelPackage travelPackage = new TravelPackage(customerId, startDate, duration, selectedcus, selectedAccommodation);
                packages.add(travelPackage);
                selectedAccommodation.setAvailable(false);
                System.out.println("Package created and added successfully.");
            } else {
                System.out.println("Invalid accommodation ID or accommodation not available.");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    // 6、列出所有订单
    public void listPackages() {
        System.out.println("List of Packages:");
        for (TravelPackage travelPackage : packages) {
            System.out.println(travelPackage.toString());
        }
    }

    // 根据id找住宿
    private Accommodation findAccommodationById(int id) {
        for (Accommodation accommodation : accommodations) {
            if (accommodation.getId() == id) {
                return accommodation;
            }
        }
        return null;
    }
    private Customer findCustom(int id){
        for (Customer cus : customers){
            if (cus.getId()==id){
                return cus;
            }
        }
        return null;
    }
    // 7、订单加lift passes
    public void addLiftPassToPackage() {
        try {
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter package ID: ");
            int packageId = scanner.nextInt();
            TravelPackage travelPackage = findPackageById(packageId);

            if (travelPackage != null) {
                System.out.print("Enter number of days for the lift passes: ");
                int day = scanner.nextInt();
                double coast=0;
                if (day == 30) {
                    coast = 200;
                }else if (day>=5){
                    coast = day*26 - day * 26 * 0.10; // 10% discount for buying 5 or more lift passes
                }else{
                    coast = day * 26;
                }
                travelPackage.setLiftPasses();
                System.out.println("Lift passes added successfully.Price="+coast);
            } else {
                System.out.println("Package not found.");
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    // 8、课程费用加到订单中
    public void addLessonFeesToPackage() {
        try {
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter package ID: ");
            int packageId = scanner.nextInt();
            TravelPackage travelPackage = findPackageById(packageId);

            if (travelPackage != null) {
                System.out.print("Enter number of lessons: ");
                int count = scanner.nextInt();
                travelPackage.addLessons(count);

//            System.out.println(travelPackage);
//            System.out.println(travelPackage.getCustomer().getLevel());
                String lessonCustom = travelPackage.getCustomer().getLevel();
                double lessonCoast = 0;
                switch (lessonCustom) {
                    case "beginner":
                        lessonCoast = count*25;
                        break;
                    case "intermediate":
                        lessonCoast = count*20;
                        break;
                    case "expert":
                        lessonCoast = count*15;
                        break;
                    default:
                        lessonCoast = 0;
                }
                System.out.println("Lessons added successfully.lesson coast="+lessonCoast);
            } else {
                System.out.println("Package not found.");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    // 公用方法
    private TravelPackage findPackageById(int id) {
        for (TravelPackage travelPackage : packages) {
            if (travelPackage.getId() == id) {
                return travelPackage;
            }
        }
        return null;
    }
    // 910存文件

    public void savePackage() {
        try {
            fos = new FileOutputStream("package.dat");
            oos = new ObjectOutputStream(fos);
            for (TravelPackage travelPackage :packages) {
                oos.writeObject(travelPackage);
            }
            fos.close();
            oos.close();
            System.out.println("successfully");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void readPackage() {
        packages.clear();
        try {
            fis = new FileInputStream("package.dat");
            ois = new ObjectInputStream(fis);

            while (true) {
                try {
                    Object object = ois.readObject();
                    TravelPackage travelPackage = (TravelPackage)object;
                    packages.add(travelPackage);
                    System.out.println(travelPackage);
                } catch (EOFException eof) {
                    fis.close();
                    ois.close();
                    break;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

